<?php
class TextTranslation extends Dao
{
	private static $_instance = null;
	protected $_db = null, $_cache = null;
	
	protected function __construct()
	{
		parent::__construct();
	}
	
	public static function getInstance() 
    {
        if (null === self::$_instance) {
            self::$_instance = new self();
        }
        return self::$_instance;
    }
	
	
	public function getList(array $args = array(), $intPage = 1, $intLimit = MIN_ROWS_PER_PAGE)
	{
		try{
			$defaults = array(
				'language' => null,
				'section' => null
			);
			$args = $this->args_merge($defaults, $args);
			
			$intStart = ($intPage-1)*$intLimit;
			
			$arrResults = $this->_db->createCommand()
									->select('text_source, text_result')
									->from('tbl_text_translation')
									->where('language_code=:lang AND section=:section', array(':lang'=>$args['language'], ':section'=>$args['section']))
									->limit($intStart, $intLimit)
									->queryAll();
			if(!empty($arrResults)){
				$arrResults[0]['total_rows'] = $this->_db->createCommand()
														 ->select('text_source, text_result')
														 ->from('tbl_text_translation')
														 ->where('language_code=:lang AND section=:section', array(':lang'=>$args['language'], ':section'=>$args['section']))
														 ->queryScalar();
			}
			return $arrResults;
		} catch(Exception $ex){
			throw new Exception($ex->getMessage());
		}
	}
	
	public function insert(array $args)
	{		
		try{
			$defaults = array(
				'language' => null,
				'section' => null,
				'text_source' => null,
				'text_result' => null
			);
			$args = $this->args_merge($defaults, $args);
			
			$cmd = $this->_db->createCommand();
			$cmd->insert('tbl_text_translation',$args);
			return Yii::app()->db->getLastInsertId();
		} catch(Exception $ex){
			throw new Exception($ex->getMessage());
		}
	}
	
	public function update($id, array $args)
	{		
		try{
			$defaults = array(
				'language' => null,
				'section' => null,
				'text_source' => null,
				'text_result' => null		
			);
			$args = $this->args_merge($defaults, $args);
			
			$cmd = $this->_db->createCommand();
			return $cmd->update('tbl_text_translation',$args, 'text_id=:id', array(':id'=>$id));
		} catch(Exception $ex){
			throw new Exception($ex->getMessage());
		}
	}
	
	public function delete($id){
		try{
			$cmd = $this->_db->createCommand();
			return $cmd->delete('tbl_text_translation', 'text_id=:id', array(':id'=>$id));			
		} catch(Exception $ex){
			throw new Exception($ex->getMessage());
		}
	}
}